import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	hidden_source: string;
	hidden_source_feature_no: string;
	hidden: {
		hidden_source_feature_no: string;
		hidden_source_feature_name: string;
		hidden_source_feature_code: string;
		control_data: string;
	};
	filter: {
		companyid: string;
		companyname: string;
		factory_no: string;
		factory_name: string;
		status_manage: string;
	};
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf-hManager\zj-000008\s002,action_id:' + action_id);

	// 查询初始化时的数据（参考第一个项目的zj-000005中的s002)
	const { prepare, exec: e } = query(action_id, session_id, systemid);
	prepare('hazard_source_feature', ['hidden_source_feature_no', 'hidden_source_feature_name', 'hidden_source_feature_code', 'control_data', 'companyid', 'companyname', 'factory_no', 'factory_name', 'status_manage'], { productid, hidden_source_feature_no: message.hidden_source_feature_no }, 1, 1, [], []);

	const [res] = await e<{ companyid: string; companyname: string; factory_no: string; factory_name: string; status_manage: string; hidden_source_feature_no: string; hidden_source_feature_name: string; hidden_source_feature_code: string; control_data: string; }>();
	// 修改隐患源特性数据
	const { exec, insert, update } = operate(action_id, session_id, systemid);
	update('hazard_source_feature', { productid, hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden_source_feature_no }, {
		companyid: message.filter.companyid,
		companyname: message.filter.companyname,
		factory_no: message.filter.factory_no,
		factory_name: message.filter.factory_name,
		hidden_source_feature_no: message.hidden.hidden_source_feature_no,
		hidden_source_feature_name: message.hidden.hidden_source_feature_name,
		hidden_source_feature_code: message.hidden.hidden_source_feature_code,
		status_manage: message.filter.status_manage,
		control_data: message.hidden.control_data
	});
	// 获取系统当前时间
	const now_date = new Date().getTime(); // 1551167783406
	// 插入管理数据表数据
	res.forEach((n) => {
		if (n.hidden_source_feature_no !== message.hidden.hidden_source_feature_no) {
			insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "隐患源特性代码", change_before: n.hidden_source_feature_code, change_after: message.hidden.hidden_source_feature_code, change_personnel: "weiwei", change_date: now_date, productid });
			insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "隐患源特性名称", change_before: n.hidden_source_feature_name, change_after: message.hidden.hidden_source_feature_name, change_personnel: "weiwei", change_date: now_date, productid });
			insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "控制数据", change_before: n.control_data, change_after: message.hidden.control_data, change_personnel: "weiwei", change_date: now_date, productid });
		} if (n.companyid !== message.filter.companyid) {
			insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "公司名称", change_before: n.companyname, change_after: message.filter.companyname, change_personnel: "weiwei", change_date: now_date, productid });
		} if (n.factory_no !== message.filter.factory_no) {
			insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "工厂名称", change_before: n.factory_name, change_after: message.filter.factory_name, change_personnel: "weiwei", change_date: now_date, productid });
		} if (n.status_manage !== message.filter.status_manage) {
			insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "状态管理", change_before: n.status_manage, change_after: message.filter.status_manage, change_personnel: "weiwei", change_date: now_date, productid });
		}
	});

	const r = await exec();

	log('Service end path:f:\实习\kh\src\wyf-hManager\zj-000008\s002,action_id:' + action_id);
	return {
		data: r
	};
}
